<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Cookie;
use think\Db;
class About extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();

        $param = $this->request->param();
        $lanmu = isset($param['lanmu'])?$param['lanmu']:null;
        $id = isset($param['id'])?$param['id']:null;
        $lang = Cookie::get('lang');
        
        $erlist = Db::name('about')->where('status',$lanmu)->order('weigh','desc')->order('id','desc')->select();


        $this->lanmu = $lanmu;
        $this->id = $id;
        $this->lang =$lang;

        $this->view->assign('lanmu', $lanmu);
        $this->view->assign('id', $id);
        $this->view->assign('erlist', $erlist);

    }

    public function index()
    {
        $keycontent='content';
        if($this->lang=='en'){
            $keycontent='content_en';
        }
       
        $id = $this->id;
        $row = Db::name('lanmu')->where('id',$id)->find();

        $section['sushuo1'] = Db::name('danye')->where('id',27)->find();
        $section['gushi'] = Db::name('danye')->where('id',21)->find();

        $row['content'] = $this->_replace_content_image($row[$keycontent]);
        $this->view->assign('row', $row);
        $this->view->assign('section', $section);
        return $this->view->fetch();
    }

    public function danye_show() {
        $keycontent='content';
        if($this->lang=='en'){
            $keycontent='content_en';
        }
        
        $id = $this->id;
        $row = Db::name('danye')->where('id',$id)->find();

        $row['content'] = $this->_replace_content_image($row[$keycontent]);
        $this->view->assign('row', $row);
        return $this->view->fetch('danye_show');
    }

    public function aboutnewshow()
    {

        $param = $this->request->param();
        $cid = isset($param['cid'])?$param['cid']:null;
        $newsid = isset($param['newsid'])?$param['newsid']:null;

        $show = Db::name('newslist')->where('id',$newsid)->find();
        

        $show['prev'] = Db::name('newslist')->where('weigh','>=',$show['weigh'])->where('id','neq',$show['id'])->where('newstype',$cid)->where('shswitch',1)->order('weigh','asc')->order('id','asc')->find();
        $show['next'] = Db::name('newslist')->where('weigh','<=',$show['weigh'])->where('id','neq',$show['id'])->where('newstype',$cid)->where('shswitch',1)->order('weigh','desc')->order('id','desc')->find();

        $this->view->assign('show', $show);
        $this->view->assign('cid', $cid);

        return $this->view->fetch('aboutnewshow');
    }  



    public function addgbook(){
        $param = $this->request->post();
        $name = isset($param['name'])?$param['name']:null;
        $title = isset($param['title'])?$param['title']:null;
        $zuoji = isset($param['zuoji'])?$param['zuoji']:null;
        $chengshi = isset($param['chengshi'])?$param['chengshi']:null;
        $tel = isset($param['tel'])?$param['tel']:null;
        $email = isset($param['email'])?$param['email']:null;
        $address = isset($param['address'])?$param['address']:null;
        $content = isset($param['content'])?$param['content']:null;

        $data['name'] = $name;
        $data['tel'] = $tel;
        $data['email'] = $email;
        $data['address'] = $address;
        $data['content'] = $content;
        $data['chengshi'] = $chengshi;
        $data['zuoji'] = $zuoji;
        $data['title'] = $title;
        $data['createtime'] = time();

        $sql = Db::name('gbooklist')->insert($data);
        if($sql){
            $result['code'] = 1;
        }else{
            $result['code'] = 0;
        }
        return $result;

    }

    private function _replace_content_image($str) {
        $dirfrom = 'uploads';
        $cdnurl = \think\Config::get('upload.cdnurl');
        $searches = [
            'src="http://118.195.172.104:8006/' . $dirfrom,
            'src="http://120.55.66.100:8082/' . $dirfrom,
            'src="http://susuojewelry.com/' . $dirfrom,
            'src="/' . $dirfrom
        ];

        return str_replace($searches,  'src="'.rtrim($cdnurl, '/') . '/' . $dirfrom, $str);
    }







    
}
